﻿using Mapster;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZR.Model.Models.ProductionControl;

namespace ImportPlcPointPosition
{
    public partial class OP50TestControl : UserControl
    {
        public OP50TestControl()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 关联的点位控件
        /// </summary>
        public AntdUI.Table PointTable { get; set; }

        private void btnStart_Click(object sender, EventArgs e)
        {
            var datas = PointTable.DataSource as List<ImportData>;
            var plcPoints = datas.Adapt<List<CksPlcDataPosition>>();
            var codePoint = plcPoints.Find(a => a.StepId == 1);
            var startWork = plcPoints.Find(a => a.StepId == 2);

            GlobalIoc.PlcClient.SetValue(codePoint, $"BETA{DateTime.Now.ToString("yyyyMMddHHmmss")}");
            GlobalIoc.PlcClient.SetValue(startWork, "1");

        }

        private void btnResetWork_Click(object sender, EventArgs e)
        {
            var datas = PointTable.DataSource as List<ImportData>;
            var plcPoints = datas.Adapt<List<CksPlcDataPosition>>();
            var codePoint = plcPoints.Find(a => a.StepId == 1);
            var startWork = plcPoints.Find(a => a.StepId == 2);
            var allowWork = plcPoints.Find(a => a.StepId == 3);
            //读取允许开工信号，如果是1就复位。
            var allowValue = GlobalIoc.PlcClient.GetStringValue(allowWork);
            if(allowValue.Equals("1"))
            {
                //复位
                GlobalIoc.PlcClient.SetValue(codePoint, $"");
                GlobalIoc.PlcClient.SetValue(startWork, "0");
            }
        }
    }
}
